//
//  ViewController.m
//  TNS
//
//  Created by parkin on 16/1/18.
//  Copyright © 2016年 zhongyan. All rights reserved.
//

#import "ViewController.h"
#import "Reachability.h"
#import "Config.h"

#import "AppDelegate.h"

#define K_ScreenWidth    [UIScreen mainScreen].bounds.size.width
#define K_ScreenHeight   [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
@property (nonatomic,strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation ViewController

@synthesize _progressView;
@synthesize _progressProxy;

- (UIWebView *)webView {
    if (!_webView) {
        CGRect rect = CGRectMake(0, 0, K_ScreenWidth, K_ScreenHeight);
        _webView = [[UIWebView alloc] initWithFrame:rect];
    }
    return _webView;
}

-(void)setUpWebView
{
    _progressProxy = [[NJKWebViewProgress alloc] init];
    self.webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.autoyou.com.cn/home"]]; // 定义请求地址
    [request setValue:self.token forHTTPHeaderField:@"X-CSRF-TOKEN"];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    self.title = @"悠享车生活";
//    self.navigationController.navigationBar.alpha = 0.000;
    self.navigationItem.hidesBackButton = YES;
    Reachability *r= [Reachability reachabilityForInternetConnection];
    if ([r currentReachabilityStatus] == NotReachable) {
        [Config showMessage:@"您当前网络不给力,请稍后再试!"];
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    dispatch_async(dispatch_get_main_queue(), ^{
    //        [NSThread sleepForTimeInterval:1.5];
    //
    //    });
    [self setUpWebView];
    [self.navigationController.navigationBar addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    //self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

//几个代理方法

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"webViewDidStartLoad");
    
}

- (void)webViewDidFinishLoad:(UIWebView *)web{
    
    NSLog(@"webViewDidFinishLoad");
    
}

-(void)webView:(UIWebView*)webView  DidFailLoadWithError:(NSError*)error{
    
    NSLog(@"DidFailLoadWithError");
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
